package com.sicheng.lc.周赛.分类.思维;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/25 16:49
 */
public class 找到指定长度的回文数 {
    StringBuilder num = new StringBuilder();

    //https://leetcode.cn/problems/find-palindrome-with-fixed-length/
    public long[] kthPalindrome(int[] queries, int intLength) {
        int n = intLength + 1 >> 1;
        int min = (int) Math.pow(10, n - 1);
        long max = (long) Math.pow(10, intLength) - 1;
        long[] nums = new long[queries.length];
        for (int i = 0; i < queries.length; i++) {
            nums[i] = get(min + queries[i] - 1, n, intLength);
            if (nums[i] > max)
                nums[i] = -1;
        }
        return nums;
    }

    private long get(int x, int n, int l) {
        num.delete(0, num.length());
        num.append(x);
        if ((l & 1) == 1) {
            x /= 10;
        }
        while (x > 0) {
            num.append(x % 10);
            x /= 10;
        }
        return num.length() > 15 ? -1 : Long.parseLong(num.toString());
    }
}
